import { Effect, Reducer, Subscription } from 'umi';
import { getClassManage,getRoomManage,getStudentManage,addClass,addRoom,deleteRoom,deleteStudent, deleteClass,getCourseManage,editClass } from '@/services/class'
import { classGrade,classRoom,classStudent,courseInt } from '@/interface/class'

export interface ClassModelState {
    classList: classGrade[],
    roomList: classRoom[],
    studentList: classStudent[],
    courseList: courseInt[]
}

export interface ClassModelType {
  namespace: 'classManage';
  state: ClassModelState;
  effects: {
    classManage: Effect;  //获取班级
    addClassList: Effect;  //添加班级
    deleteClass: Effect;  //删除班级
    editClassList: Effect; //修改班级
    roomManage: Effect;  //获取教室
    addRoomList: Effect;  //添加教室
    deleteRoomList: Effect; //删除教室
    studentManage: Effect;  //获取学生
    deleteStudent: Effect;  //删除学生
    getCourseList: Effect;  //获取所有课程

  }; 
  reducers: {
    save: Reducer<ClassModelState>;
  };
  subscriptions: { setup: Subscription };
}

const ClassModel: ClassModelType = {
  namespace: 'classManage',

  state: {
    classList: [],
    roomList: [],
    studentList: [],
    courseList: []
  },

  effects: {
    //班级管理
    *classManage({ payload }, { call, put }) {
        let result = yield call(getClassManage)
        if(result.code===1){
            yield put({
                type: 'save',
                payload: {classList: result.data}
            })
        }
    },
    //添加班级
    *addClassList({ payload }, { call, put }){
      console.log(payload)
      let data = yield call(addClass,payload)
      console.log(data)
      if(data.code==1){
        yield put({
          type: 'classManage'
        })
      }
    },
    //删除班级
    *deleteClass({ payload },{ call, put }){
      // console.log(payload)
      let data = yield call(deleteClass,payload)
      if(data.code===1){
        yield put({
          type: 'classManage'
        })
      }
    },
    //修改班级
    *editClassList({ payload },{ call, put }){
      console.log(payload)
      let data = yield call(editClass,payload)
      if(data.code===1){
        yield put({
          type: 'classManage'
        })
      }
    },
    //获取所有课程
    *getCourseList({ payload },{ call, put }){
      let data = yield call(getCourseManage)
      if(data.code===1){
        yield put({
          type: 'save',
          payload: {
            courseList: data.data
          }
        })
      }
      
    },
    //教室管理
    *roomManage({ payload }, { call, put }){
        let data = yield call(getRoomManage)
        if(data.code===1){
          yield put({
            type: 'save',
            payload: {roomList: data.data}
          })
        }
    },
    //添加教室
    *addRoomList({ payload }, { call, put }){
      let data = yield call(addRoom,payload)
      console.log(data)
      if(data.code===1){
        yield put({
          type: 'roomManage'
        })
      }
    },
    //删除教室
    *deleteRoomList({ payload }, { call, put }){
      let data = yield call(deleteRoom,payload)
      if(data.code===1){
        yield put({
          type: 'roomManage'
        })
      }
    },
    //学生管理
    *studentManage({ payload }, { call, put }){
      let result = yield call(getStudentManage)
      if(result.code===1){
        yield put({
          type: 'save',
          payload: {studentList: result.data}
        })
      }
    },
    //删除学生
    *deleteStudent({ payload }, { call, put }){
      console.log(payload)
      let data = yield call(deleteStudent,payload.id)
      if(data.code==1){
        yield put({
          type: 'studentManage'
        })
      }
    }
  },
  reducers: {
    save(state, action) {
      return {
        ...state,
        ...action.payload,
      };
    },
  },
  subscriptions: {
    setup({ dispatch, history }) {
      return history.listen(({ pathname }) => {
        if (pathname === '/') {
          dispatch({
            type: 'query',
          })
        }
      });
    }
  }
};

export default ClassModel;
